package io.github.hadyang.leetcode.offer;

import io.github.hadyang.leetcode.ListNode;

/** @author haoyang.shi */
public class FindKthToTail {

  public ListNode FindKthToTail(ListNode head, int k) {
    if (head == null) {
      return null;
    }

    ListNode cursor = head;
    ListNode cursorK = head;

    int i = 0;

    while (cursorK != null) {
      cursorK = cursorK.next;

      if (i >= k) {
        cursor = cursor.next;
      }

      i++;
    }

    if (i < k) {
      return null;
    }

    return cursor;
  }
}
